package com.atguigu.demo2_mybatis.service;

import com.atguigu.demo2_mybatis.mapper.UserMapper;
import jakarta.annotation.Resource;
import com.atguigu.demo2_mybatis.model.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author zhaodf
 */
@Service
public class UserService implements UserDetailsService {


    @Resource
    UserMapper userMapper;

    /**
     * 当用户登录的时候，会自动触发这个方法的执行
     *
     * @param username the username identifying the user whose data is required.
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User u = userMapper.loadUserByUsername(username);
        if (u == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return u;
    }
}
